Skip to content

DATAMONGO-1849 - Resolve JsonSchema from domain type. #733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from

Conversation

christophstrobl
Copy link
Member

@christophstrobl christophstrobl commented Mar 29, 2019

JsonSchemaCreator extracts the MongoJsonSchema for a given Class by applying the following mapping rules:

Required Properties

  • All Constructor arguments unless annotated with Nullable.
  • Properties of primitive type.

Ignored Properties

  • All properties annotated with Transient.

Property Type Mapping:

  • java.lang.Object -> { type : 'object' }
  • java.util.Arrays -> { type : 'array' }
  • java.util.Collection -> { type : 'array'}
  • java.util.Map -> { type : 'object'}
  • java.lang.Enum -> { type : 'string', enum : [ ... ] }
  • Simple Types -> { type : 'the corresponding bson type' }
  • Domain Types -> { type : 'object', properties : { ... } }

_id properties using types that can be converted into ObjectId like String will be mapped to { type : 'object' } unless there is more specific information available via the MongoId annotation.

JsonSchemaCreator extracts the MongoJsonSchema for a given Class by applying the following mapping rules:

Required Properties:
- All Constructor arguments annotated with Nullable.
- Properties of primitive type.

Ignored Properties:
- All properties annotated with Transient.

Property Type Mapping:
- java.lang.Object -> { type : 'object' }
- java.util.Arrays -> { type : 'array' }
- java.util.Collection -> { type : 'array'}
- java.util.Map -> { type : 'object'}
- java.lang.Enum -> { type : 'string', enum : [ ... ] }
- Simple Types -> { type : 'the corresponding bson type' }
- Domain Types -> { type : 'object', properties : { ... } }

_id properties using types that can be converted into
ObjectId like String will be mapped to { type : 'object' } unless there is more specific information available via the MongoId annotation.
By using Field#targetType it is now possible to pass down a type hint to the conversion subsystem. This allows specifying the desired target type for a property so that eg. a plain String can be stored as Code.
@christophstrobl christophstrobl marked this pull request as ready for review April 4, 2019 06:50

private boolean isRequiredProperty(PersistentEntity<?, ?> parent, PersistentProperty property) {

return (parent.isConstructorArgument(property) && !property.isAnnotationPresent(Nullable.class))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See NullableUtils: Properties are nullable by default. Annotating a package with @NonNullApi or @NonNullFields switches to non-null by default.

* @param mongoConverter must not be {@literal null}.
* @return new instance of {@link MongoJsonSchemaCreator}.
*/
default MongoJsonSchemaCreator jsonSchemaCreator(MongoConverter mongoConverter) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be static?

@@ -1667,4 +1689,91 @@ private static boolean isCollectionOfDbRefWhereBulkFetchIsPossible(Iterable<?> s
return null;
}
}

private static class HijackedTypeInformation<S> implements TypeInformation<S> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about TypeInformationWrapper 😬and handling the rest of customizations in a separated, smaller type?

* @return never {@literal null}.
* @since 2.2
*/
default Class<?> computeWriteTarget(Class<?> source) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should improve either on the definition or on the implementation of this method. Right now, we could assume that the write target is either a native Mongo type or the write target is an to be converted type.

The number of exposed methods via MongoWriter and MongoConverter keeps constantly increasing and I'm not sure how to think about it.

christophstrobl and others added 3 commits April 8, 2019 13:40
Fix generics usage in MappingMongoJsonSchemaCreator. Make fields final. Rename MappingMongoConverter.computeWriteTarget to getWriteTarget and expose it publicly for reuse in custom DefaultTypeMapper setups without the need to subclass MappingMongoConverter.

Remove Nullability functionality for required fields as nullability indicators should originate from PersistentProperty and PreferredConstructor.

Related ticket: DATACMNS-1513
mp911de pushed a commit that referenced this pull request Apr 9, 2019
MongoJsonSchemaCreator extracts the MongoJsonSchema for a given Class by applying the following mapping rules:

Required Properties:
- Properties of primitive type.

Ignored Properties:
- All properties annotated with Transient.

Property Type Mapping:
- java.lang.Object -> { type : 'object' }
- java.util.Arrays -> { type : 'array' }
- java.util.Collection -> { type : 'array'}
- java.util.Map -> { type : 'object'}
- java.lang.Enum -> { type : 'string', enum : [ ... ] }
- Simple Types -> { type : 'the corresponding bson type' }
- Domain Types -> { type : 'object', properties : { ... } }

_id properties using types that can be converted into
ObjectId like String will be mapped to { type : 'object' } unless there is more specific information available via the MongoId annotation.

By using Field#targetType it is now possible to pass down a type hint to the conversion subsystem. This allows specifying the desired target type for a property so that eg. a plain String can be stored as Code.

Original pull request: #733.
mp911de added a commit that referenced this pull request Apr 9, 2019
Fix generics usage in MappingMongoJsonSchemaCreator. Make fields final. Rename MappingMongoConverter.computeWriteTarget to getWriteTarget and expose it publicly for reuse in custom DefaultTypeMapper setups without the need to subclass MappingMongoConverter.

Remove Nullability functionality for required fields as nullability indicators should originate from PersistentProperty and PreferredConstructor. Update documentation.

Related ticket: DATACMNS-1513
Original pull request: #733.
@mp911de
Copy link
Member

mp911de commented Apr 9, 2019

That's merged and polished now.

@@ -75,7 +76,9 @@ static Collection<Object> getConvertersToRegister() {
List<Object> converters = new ArrayList<>();

converters.add(BigDecimalToStringConverter.INSTANCE);
converters.add(BigDecimalToDecimal128Converter.INSTANCE);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has no effect, as BigDecimalToStringConverter.INSTANCE from above always takes precedence. All BigDecimal end up as String in the database.

Why do we still need BigDecimalToStringConverter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants